package com.lg.utils.hutool.util;

import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.TypeUtil;
import com.lg.utils.dto.User;


import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Java的反射机制
 *
 * TypeUtil
 *    获取方法的参数和返回值类型
 */
public class ReflectUtilTest {
    public class TestClass {
        public List<String> getList(){
            return new ArrayList<>();
        }

        public Integer intTest(Integer integer) {
            return 1;
        }
    }


    public static void main(String[] args) {
        Method[] methods = ReflectUtil.getMethods(User.class);
        Method method = ReflectUtil.getMethod(User.class, "getUserId");
        User user = ReflectUtil.newInstance(User.class);
        ReflectUtil.invoke(user, "setUserId", 10);


        Method method2 = ReflectUtil.getMethod(TestClass.class, "intTest", Integer.class);
//        class java.lang.Integer
        System.out.println(TypeUtil.getParamType(method2, 0));

//        null
        System.out.println(TypeUtil.getParamType(method2, 2));

//        class java.lang.Integer
        Type returnType = TypeUtil.getReturnType(method2);
        System.out.println(returnType);

//        null
        System.out.println(TypeUtil.getTypeArgument(returnType));


        Method method3 = ReflectUtil.getMethod(TestClass.class, "getList");
        Type type = TypeUtil.getReturnType(method3);
        Type type2 = TypeUtil.getTypeArgument(type);
//        class java.lang.String
        System.out.println(type2);

    }




}
